using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
using System.Text;

namespace marbletrack
{
    static class Audio
    {
        
        // Audio objects
        static public AudioEngine engine;
        static public SoundBank soundBank;
        static public WaveBank waveBank;

        // 3D audio objects
        static public float marbleHitVol = 1.0f;
        static public float trackHitVol = 1.0f;
        static public float marbleRollVol = 1.0f;
        static public AudioEmitter emitter_M = new AudioEmitter();
        static public AudioEmitter emitter_T = new AudioEmitter();
        static public AudioListener listener = new AudioListener();
        static public Cue marbleHitCue;
        static public Cue trackHitCue;
        static public Cue marbleRollingLoop;
        static public int i = 0;
        static public AudioCategory trackHitCategory;
        static bool playingMarbleHit = false;
        static bool playingTrackHit = false;
        static bool playingMarbleRoll = false;

        static public void update()
        {
            i = 0;
            if (playingMarbleRoll)
            {
                if (!marbleRollingLoop.IsPlaying)
                {
                    marbleRollingLoop = soundBank.GetCue("Marble Rolling on Wood Loop");
                    marbleRollingLoop.Play();
                }
                playingMarbleRoll = false;
            }
            else if (marbleRollingLoop.IsPlaying)
            {
                marbleRollingLoop.Stop(AudioStopOptions.Immediate);
            }

            if (playingTrackHit)
            {
                Audio.trackHitCategory.SetVolume(trackHitVol);
                Audio.trackHitCue = Audio.soundBank.GetCue("Marble hittin wood");
                Audio.trackHitCue.Apply3D(Audio.listener, Audio.emitter_T);
                Audio.trackHitCue.Play();
                playingTrackHit = false;
            }

            if (playingMarbleHit)
            {
                Audio.marbleHitCue = Audio.soundBank.GetCue("marble hitting marble");
                Audio.marbleHitCue.Apply3D(Audio.listener, Audio.emitter_M);
                Audio.marbleHitCue.Play();
                playingMarbleHit = false;
            }

            // Update the audio engine
            engine.Update();
        }

        static public void PlayMarbleHit()
        {
            playingMarbleHit = true;
        }

        static public void PlayTrackHit()
        {
            playingTrackHit = true;
        }

        static public void PlayMarbleRoll()
        {
            playingMarbleRoll = true;
        }

        static public void initialize()
        {
            // Initialize audio objects.
            engine = new AudioEngine("Content\\Audio\\Marbletracksounds.xgs");
            soundBank = new SoundBank(engine, "Content\\Audio\\Sound Bank.xsb");
            waveBank = new WaveBank(engine, "Content\\Audio\\Wave Bank.xwb");
            trackHitCategory = engine.GetCategory("trackHitCategory");
            trackHitCue = soundBank.GetCue("Marble hittin wood");
            marbleHitCue = soundBank.GetCue("marble hitting marble");
            marbleRollingLoop = soundBank.GetCue("Marble Rolling on Wood Loop");
        }

    }
}
